import { http } from "@/utils/http";
import type { AjaxResult } from "./types";
import { baseUrlApi } from "./utils";

export const listAllProduct = () => {
  return http.request<AjaxResult>("get", baseUrlApi("/product/listAll"));
};

export const listProductByPage = (params?: QueryParams) => {
  return http.request<AjaxResult>("get", baseUrlApi("/product/page"), {
    params
  });
};

export const getProductById = (productId: number) => {
  return http.request<AjaxResult>("get", baseUrlApi("/product/" + productId));
};

export const addProduct = (data: ProductForm) => {
  return http.request<AjaxResult>("post", baseUrlApi("/product"), { data });
};

export const updateProduct = (data: ProductForm) => {
  return http.request<AjaxResult>("put", baseUrlApi("/product"), { data });
};

export const deleteProduct = (productId: number) => {
  return http.request<AjaxResult>(
    "delete",
    baseUrlApi("/product/" + productId)
  );
};

export const deleteProductBatch = (productIds: number[]) => {
  return http.request<AjaxResult>(
    "delete",
    baseUrlApi("/product/delBatch/" + productIds)
  );
};

interface CategoryDTO {
  categoryId: number;
  categoryName: string;
}

// 查询参数数据
interface QueryParams {
  pageNum: number;
  pageSize: number;
  productSn?: string;
  productName?: string;
  categoryId?: number;
}

// 商品列表数据
interface ProductDTO {
  productId: number;
  productSn: string;
  productName: string;
  price: number;
  categoryId: number;
  category: CategoryDTO;
}

// 商品表单数据
interface ProductForm {
  productId?: number;
  productSn?: string;
  productName?: string;
  categoryId?: number;
  price: number;
}

export type { CategoryDTO, QueryParams, ProductDTO, ProductForm };
